/*
 * ProxySession.h
 *
 *  Created on: Apr 23, 2012
 *      Author: shaohong
 */

#ifndef PROXYSESSION_H_
#define PROXYSESSION_H_

#include <vector>
#include "HttpRequest.h"
#include "ProxyServer.h"

using namespace std;

class ProxySession {
public:

	/**
	 * Constructor for ProxySession.
	 * proxyServer refers to the proxyServer that this proxySession is serving.
	 * connFD is the connection from where the HTTP request came from.
	 * httpRequest is the request to be served.
	 */
	ProxySession(ProxyServer * proxyServer, int connFD, HttpRequest * httpRequest);
	virtual ~ProxySession();

	void doProxy();

private:
	ProxyServer * proxyServer;

	HttpRequest * httpRequest;	// the httpRequest the creates this proxySession

	int upstreamConn;	// the connFD with client side
    void processHeaders();

	/**
	 * @brief handles the "Hop-Count" header
	 * @return 0: proxy shall continue;
	 * 	-1: proxy shall terminate;
	 */
	int processHopCount();

    void doForwardProxy();

	void doReverseProxy();

	void doReverseProxyGET();

	void doReverseProxyPUT();

	void sendResponse(HttpResponse * response);

	void sendErrorResponse(int responseCode, string reasonPhrase);

	void doLocalGET(string localFilePath);

};

#endif /* PROXYSESSION_H_ */
